{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import MiniBatchKMeans  # 导包不同\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "data = np.genfromtxt(\"kmeans.txt\", delimiter=\" \")\n",
    "# 设置k值\n",
    "k = 4  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MiniBatchKMeans(batch_size=100, compute_labels=True, init='k-means++',\n",
       "                init_size=None, max_iter=100, max_no_improvement=10,\n",
       "                n_clusters=4, n_init=3, random_state=None,\n",
       "                reassignment_ratio=0.01, tol=0.0, verbose=0)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "model = MiniBatchKMeans(n_clusters=k)\n",
    "model.fit(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.62579126  3.16811604]\n",
      " [-3.38899405 -2.93471434]\n",
      " [-2.45512194  2.75949312]\n",
      " [ 2.75769024 -2.73873852]]\n"
     ]
    }
   ],
   "source": [
    "# 分类中心点坐标\n",
    "centers = model.cluster_centers_\n",
    "print(centers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0\n",
      " 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 0 2\n",
      " 3 1 0 2 3 1]\n"
     ]
    }
   ],
   "source": [
    "# 预测结果\n",
    "result = model.predict(data)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAbqUlEQVR4nO3dfWxcV5kG8Ocdx3b8sU3VxHKhjT21CBXZfFDVQgSEtksaVEppF4qqwiSiVMIqBamh6XYJhkUlsrZKJChoKStrt1VKR4KENttNFcRCQrRaKaziAm6bmH4oa4dC17gpLQl2HNvz7h/Xk3jGd8Z35n6dc+/zk6yJr8d3znjaZ86c+55zRFVBRET2ysTdACIi8odBTkRkOQY5EZHlGORERJZjkBMRWW5ZHA+6atUqzWazcTw0EZG1nnvuuTdUtaP8eCxBns1mMTQ0FMdDExFZS0TG3I5zaIWIyHIMciIiyzHIiYgsxyAnIrIcg5yIyHIMciJy5PNANgtkMs5tPh93i8gjBjkROaHd1weMjQGqzm1fX7LCPMFvVAxyIgL6+4HJydJjk5PO8SRI+BsVg5yIgNOnaztum4S/UTHIiQjo6qrtuG0S/kbFICciYGAAaG0tPdba6hxPgoS/UTHIiQjI5YDBQaC7GxBxbgcHneNJkPA3qlgWzSIiA+VyyQnucsXn1d/vDKd0dTkhnpDnyyAnonRI8BsVh1aIiCzHICcishyDnIjIcgxyIiLLMciJiCzHICcishyDnIjIcgxyIiLLMciJiCzHICcishyDnChNErxLTpoxyCk0+RfyyD6SReahDLKPZJF/gaERq4TvkpNmDHIKRf6FPPoO9mHs7TEoFGNvj6HvYB/DPE4J3yUnzRjkFIr+w/2YnCkNjcmZSfQfZmjEJuG75KQZg5xCcfpt93CodJwikPBdctKMQU6h6FrhHg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ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "\n",
    "# 画出各个分类的中心点\n",
    "mark = ['*r', '*b', '*g', '*y']\n",
    "for i,center in enumerate(centers):\n",
    "    plt.plot(center[0],center[1], mark[i], markersize=20)\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD5CAYAAAA6JL6mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAfNUlEQVR4nO3de5Bc1X0n8O+vH6PRvEfSWK+RxEORLBtDhAUmBmFZxogAhqzLf2RdyVpxZbV5eWUnDsGhKpVUpSpUnAqhKnE2KuJls5YXsjHEcXDCI0RZsTEYAcZEFhBJZiQhCT1H89I8evqXP/oxr+7bt/ue2/ece7+fKheanub2GSF/79Hv/s45oqogIiJ3paIeABERBcMgJyJyHIOciMhxDHIiIscxyImIHMcgJyJyXMbERUSkB8AjAK4BoAA+r6rfq/b+dGe7Zpb1mvhoolC1npiMeghEZUO5s+dUtW/+60aCHMDDAP5RVT8jIi0A2rzenFnWi5W/+wVDH00Unk33H4t6CERl/3j6awOVXg8c5CLSBeBWADsBQFUnAXAaQ0TUJCZq5FcBOAvgf4rIayLyiIi0G7guUeQm398f9RCIajIR5BkA1wP4c1XdDGAUwP3z3yQiu0TkgIgcmB4eNfCxREQEmAnyEwBOqOpLxa//BoVgn0NV96jqFlXdku7khJ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xbqn1PRH4ZwBPF4P6+iORR2DfhbLPGF0S1n01EPgTgSgCviwhQKD28KiI3qurpJg6xYV7/3QBARD4H4G4An3DlxuvhBIA1s77uB3AyorEYJSJZFEJ8r6o+EfV4DLoZwD0icieAVgBdIvINVf25iMdlnPV95CLySwBWqerviMgGAP8EYG0MgmEOEXkHwBZVjcPGPhCROwD8MYCPqaoTN10vIpJB4aHtJwC8C+BlAJ9V1YORDiwgKcwi/heAC6r6xajHE5bijPzLqnp31GMJgws18q8DuEpE/g2FB0yfi1uIx9SfAugE8KyI/EBE/kfUAwqi+OD21wA8jcIDwb92PcSLbgbw8wC2F/87/aA4gyWHWD8jJyIiby7MyImIyAODnIjIcQxyIiLHMciJiBzHICcichyDnIjIcQxyIiLHMciJiBz3Hx3LkSH5znF3AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 获取数据值所在的范围\n",
    "x_min, x_max = data[:, 0].min() - 1, data[:, 0].max() + 1\n",
    "y_min, y_max = data[:, 1].min() - 1, data[:, 1].max() + 1\n",
    "\n",
    "# 生成网格矩阵\n",
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),\n",
    "                     np.arange(y_min, y_max, 0.02))\n",
    "\n",
    "z = model.predict(np.c_[xx.ravel(), yy.ravel()])# ravel与flatten类似，多维数据转一维。flatten不会改变原始数据，ravel会改变原始数据\n",
    "z = z.reshape(xx.shape)\n",
    "# 等高线图\n",
    "cs = plt.contourf(xx, yy, z)\n",
    "# 显示结果\n",
    "# 画出各个数据点，用不同颜色表示分类\n",
    "mark = ['or', 'ob', 'og', 'oy']\n",
    "for i,d in enumerate(data):\n",
    "    plt.plot(d[0], d[1], mark[result[i]])\n",
    "\n",
    "# 画出各个分类的中心点\n",
    "mark = ['*r', '*b', '*g', '*y']\n",
    "for i,center in enumerate(centers):\n",
    "    plt.plot(center[0],center[1], mark[i], markersize=20)\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
